package com.zgzg.shop.pay.base.context;

import com.zgzg.shop.pay.base.dto.JwtUserDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class RequestContext {

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes ra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return ra != null ? ra.getRequest() : null;
    }

    public static String getUserIP() {
        return getHeaderOrDefault("X-tmy-userIP", "127.0.0.1");
    }

    public static String getSource() {
        return getHeaderOrDefault("X-tmy-media-source", "1001");
    }

    public static String getHeaderOrDefault(String headerName, String defaultHeader) {
        HttpServletRequest request = getRequest();
        if (request != null) {
            String header = request.getHeader(headerName);
            return StringUtils.isNotEmpty(header) ? header : defaultHeader;
        }
        return defaultHeader;
    }

    /**
     * 获取当前登录用户对象
     **/
    public static JwtUserDTO getCurrentUser() {
        HttpServletRequest request = getRequest();

        return null;
    }
}
